import api from '../../api';
import { message } from 'antd';
import moment from 'moment';
export default {
  namespace: 'productManageM',
  state: {
    isRefresh: false, //是否需要重新刷新列表
    dictMap: {}, // 未经过处理的字典值
    dictMapSelect: {}, // 用于搜索页面搜索条件的所有下拉框的字典值
    AccountConfigAllowedMarket: [], // ExtensionList.js中 交易场所的下拉list
    tradingInformationDrop: {}, //tradingInformationDrop接口返回的data
    TransactionMode: [], // ExtensionList.js中 交易模式的下拉list
  },
  effects: {
    //获取列表
    *getList({ payload, callback }, { call, put }) {
      const result = yield call(api.productMange.getList, payload);
      if (result.code == 0) {
        const response = {
          columns: result?.data?.columns || [],
          dataSource: result?.data?.dataSource || [],
          pageInfo: result?.data?.pageInfo || {
            numInPage: 10,
            currentPage: 1,
            total: 0,
          },
        };
        callback(response);
      } else {
        message.error('查询列表失败！');
        return;
      }
    },
    //查看详情
    *getDetail({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.detailInfo, payload);
      if (response && response.code == 0) {
        callback({
          baseInfo: (response?.data?.dataSource?.length && response?.data?.dataSource[0]?.baseInfo) || {}, //基本信息
          beiAnInfo: (response?.data?.dataSource?.length && response?.data?.dataSource[0]?.beiAnInfo) || {}, //备案信息
          publishInfo: (response?.data?.dataSource?.length && response?.data?.dataSource[0]?.publishInfo) || {},
        });
        yield put({
          type: 'save',
          payload: {
            currentModes: (response?.data?.dataSource?.length && response?.data?.dataSource[0]?.baseInfo).modes || {},
          },
        });
      } else {
        message.error(response?.msg || '查看详情失败！');
        return;
      }
    },
    //删除表单
    *deleteForm({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.delInfo, payload);
      if (response.success) {
        callback(true);
      } else {
        message.error(response?.mesg || '删除产品信息失败！');
        return;
      }
    },
    //保存表单内容
    *saveFn({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.savefN, payload);
      if (response.success) {
        callback(true);
      } else {
        message.error(response?.mesg || '保存失败！');
        return;
      }
    },
    // 获取ExtensionList.js 中必要的下拉list
    *tradingInformationDrop({ payload }, { call, put }) {
      const response = yield call(api.productMange.tradingInformationDrop, payload);
      if (response && response.success) {
        yield put({
          type: 'save',
          payload: {
            tradingInformationDrop: response.data,
            AccountConfigAllowedMarket: response.data.AccountConfigAllowedMarket,
            TransactionMode: response.data.TransactionMode,
          },
        });
      } else {
        message.error(`获取ExtensionList.js 中必要的下拉list数据失败，失败原因：${response?.mesg}`);
        console.error(
          `获取ExtensionList.js 中必要的下拉list数据失败，失败原因：${response?.mesg}`,
          '接口名称： trading-information-drop-down 接口：/maatProduct/trading-information-drop-down',
        );
      }
    },
    //字典key
    *fetchDictByKeys({ payload }, { call, put }) {
      const response = payload['dictData'];
      if (response) {
        let dictMapSelect = {};
        Object.getOwnPropertyNames(response).forEach(function (key) {
          if (response[key]) {
            dictMapSelect[key] = response[key].map((ele) => {
              return { value: ele.code, title: ele.value };
            });
          }
        });
        yield put({
          type: 'save',
          payload: {
            dictMap: response,
            dictMapSelect,
          },
        });
      } else {
        message.error(`查询数据字典失败，失败原因：`);
      }
    },
    //改变fresh状态
    *changeRresh({ payload }, { call, put }) {
      yield put({
        type: 'save',
        payload: {
          isRefresh: payload.status,
        },
      });
    },
    //上传模板
    *uploadFile({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.uploadFile, payload);
      if (response && response.code == 0) {
        callback(response.data?.dataSource || []);
      } else {
        callback();
        message.error(response?.msg || '上传失败!');
      }
    },
    ///查询负债端维护列表
    *getLiabilitySideInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.getLiabilitySideInfo, payload);
      if (response && response.code == 0) {
        callback(response.data?.dataSource || []);
      } else {
        callback();
        message.error(response?.msg || '查询列表失败!');
      }
    },
    ///保存负债端维护列表
    *saveLiability({ payload, callback }, { call, put }) {
      const response = yield call(api.productMange.saveLiability, payload);
      if (response && response.code == 0) {
        callback('success');
      } else {
        callback();
        message.error(response?.msg || '保存列表失败!');
      }
    },
    ///根据产品code 获取组合列表暂时没用上
    *queryPortfolioList({ payload, callback }, { call, put }) {
      const response = yield call(api.getProductGroup, payload);
      if (response && response.success) {
        let readNodes = (nodes = []) => {
          for (let item of nodes) {
            item.value = item.code;
            item.title = `${item.portfolioCode} - ${item.portfolioName}`;
            if (item.children && item.children.length) readNodes(item.children);
          }
        };
        readNodes(response.data);
        callback(response.data);
      } else {
        callback();
        message.error(response?.errorMsg || '查询组合列表失败!');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
